package org.jspresso.model.processor;

import java.util.ArrayList;
import java.util.List;

import org.jspresso.framework.application.backend.BackendControllerHolder;
import org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController;
import org.jspresso.framework.util.bean.integrity.EmptyPropertyProcessor;
import org.jspresso.model.ActionTestIo;
import org.jspresso.model.BackActionTest;
import org.jspresso.model.TestEntity;
import org.jspresso.model.TestScope;
import org.jspresso.tester.utils.TestEntitySerializer;

/**
 * Generated by Jspresso Developer Studio
 */
public class BackActionTestProcessors {

	/**
	 * generated by Jspresso Developer Studio
	 */
	public static class TestScopeProcessor extends EmptyPropertyProcessor<BackActionTest, TestScope> {
		@SuppressWarnings("unchecked")
		@Override
		public void postprocessSetter(BackActionTest target, TestScope oldPropertyValue, TestScope newPropertyValue) {
			cleanParameters((List<ActionTestIo>)(List<?>)target.getInputs(), newPropertyValue);
			cleanParameters((List<ActionTestIo>)(List<?>)target.getOutputs(), newPropertyValue);
			super.postprocessSetter(target, oldPropertyValue, newPropertyValue);
		}
		
		private void cleanParameters(List<ActionTestIo> parameters, TestScope scope) {
			for(ActionTestIo testIo : parameters) {
				TestEntity toSetTestEntity =	TestEntitySerializer.findTestEntity(testIo.getParameterRefValue(), scope, (HibernateBackendController) BackendControllerHolder.getCurrentBackendController());
				testIo.setParameterRefValue(toSetTestEntity);

				List<TestEntity> toReplace = new ArrayList<TestEntity>();
				for(TestEntity candidate : testIo.getParameterRefValues()) {
					TestEntity replacementCandidate = TestEntitySerializer.findTestEntity(candidate, scope, (HibernateBackendController) BackendControllerHolder.getCurrentBackendController());
					if(replacementCandidate != null) {
						toReplace.add(replacementCandidate);
					}
				}
				testIo.setParameterRefValues(toReplace);
			}
		}
	}

}